package com.meixiang.fragment.service;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.adapter.service.EvaluateAdapter;
import com.meixiang.entity.services.Evaluate;
import com.meixiang.entity.training.TrainingEvaluate;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 美容师详情内的 评价
 * xiejingwen
 */
public class EvaluationFragment extends BaseFragment implements OnRefreshListener,OnLoadMoreListener{

    @Bind(R.id.swipe_target)
    RecyclerView swipeTarget;
    @Bind(R.id.refresh)
    SwipeToLoadLayout refresh;

    private EvaluateAdapter mAdapter;

    private List<TrainingEvaluate> trainingEvaluates;
    private List<Evaluate> mEvaluates;
    private int mPageNo;
    private int totalPage;
    private int evalTime; //评价次数
    private String scoreAve;//评价分数
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_evaluation, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        refresh.setOnRefreshListener(this);
        refresh.setOnLoadMoreListener(this);
        swipeTarget.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter=new EvaluateAdapter(1);
        swipeTarget.setAdapter(mAdapter);
    }

    @Override
    protected void setData() {
        getEvaluateList(1,true);
        getEvalLabel();
    }
    private void getEvaluateList(int pageNo, final boolean isPull){
        HttpParams params=new HttpParams();
        params.put("beauticianId",activity.getIntent().getStringExtra("beauticianId"));
        params.put("pageNo",pageNo+"");
        HttpUtils.post(Config.BEAUTICIAN_EVALUATE, params, new HttpCallBack(activity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if (isPull){
                    mAdapter.clear();
                }
                JSONArray array=result.optJSONArray("result");
                mPageNo=result.optInt("pageNo");
                totalPage=result.optInt("totalPage");
                trainingEvaluates=new ArrayList<>();
                for (int i=0;i<array.length();i++){
                    TrainingEvaluate evaluate= AbJsonUtil.fromJson(array.opt(i).toString(),TrainingEvaluate.class);
                    trainingEvaluates.add(evaluate);
                }
                if (trainingEvaluates.size()>0){
                    mAdapter.addAll(trainingEvaluates);
                }else{
                    showNoData("暂无评价");
                }

            }
            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(activity,message);
            }

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                super.onAfter(isFromCache, jsonObject, call, response, e);
                if (refresh.isRefreshing()){
                    refresh.setRefreshing(false);
                }else{
                    refresh.setLoadingMore(false);
                }
            }
        });
    }

    private void getEvalLabel(){
        HttpParams params=new HttpParams();
        params.put("beauticianId",activity.getIntent().getStringExtra("beauticianId"));
        HttpUtils.post(Config.BEAUTICIAN_EVALUATE_LABEL, params, new HttpCallBack(activity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                Log.e("result",result.toString());
                try {
                    JSONArray array=result.optJSONArray("o2oBeauticianEvalLabels");
                    if (array!=null&&array.length()>0){
                        mEvaluates=new ArrayList<>();
                        for (int i=0;i<array.length();i++){
                            Evaluate label=new Evaluate();
                            label.setBeauticianId(array.getJSONObject(i).getString("beauticianId"));
                            label.setTagId(array.getJSONObject(i).getString("beauticianEvalLabelId"));
                            label.setTagName(array.getJSONObject(i).getString("evalLabel"));
                            label.setTagCount(array.getJSONObject(i).getString("evalLabelTime"));
                            mEvaluates.add(label);
                        }
                        mAdapter.addEvaluateData(mEvaluates);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String code, String message) {

            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onLoadMore() {
        if (mPageNo<totalPage){
            mPageNo++;
            getEvaluateList(mPageNo,false);
        }else{
            AbToastUtil.showToast(activity,"已没有更多数据");
            refresh.setLoadingMore(false);
        }
    }

    @Override
    public void onRefresh() {
        getEvaluateList(1,true);
        getEvalLabel();
    }
}
